package test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.synonym.SolrSynonymParser;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;

import com.dhgate.spu.analysis.consts.SysConsts;

public class SynoymMapDemo {

	public static void main(String[] args) throws IOException, ParseException {

		

		boolean dedup = true;
		boolean expand = false;
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		SolrSynonymParser parser = new SolrSynonymParser(dedup, expand, analyzer);

		parser.add(new FileReader(new File(SysConsts.CONST_SYNOYM_FILE_PATH)));
		SynonymMap build = parser.build();
		
//		StringReader reader = new StringReader("MT6573,MTK6276M");
		String title = "off-shoulder Split/Front Criss-croossed 2014 New Designer Ball Gown Sweetheart Beading Off The Shoulder Sleeveless Floor-Length Organza Evening Dresses";
		StringReader reader = new StringReader(title);
		TokenStream ts = analyzer.tokenStream("", reader);

		SynonymFilter synonymFilter = new SynonymFilter(ts, build, true);
		CharTermAttribute term = synonymFilter.getAttribute(CharTermAttribute.class);
		synonymFilter.reset();
		String attrVal = "Split/Front";
		String attrValTemp = "";
		String a[] = attrVal.split(" ");
		if(a.length < 2){
			a = attrVal.split("-");
		}
		if(a.length < 2){
			a = attrVal.split("/");
		}		
		if(a.length > 1){
			for(int i = 0;i < a.length;i++){
				attrValTemp = attrValTemp + a[i]+" ";
			}
			attrVal = attrValTemp;
		}
		StringBuilder rString = new StringBuilder();
		while (synonymFilter.incrementToken()) {
			if(term.toString().equals(attrVal.toLowerCase())){
				System.out.println(term.toString());
			}
			rString.append(term.toString()+" ");
		}
		if(a.length > 1 && (rString.indexOf(attrVal.toLowerCase()) != -1 ||title.toLowerCase().indexOf(attrVal.toLowerCase()) != -1)){
			System.out.println(attrVal);
		}
		System.out.println(rString);
	}
}
